﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using System.Data.Entity;
using Equin.ApplicationFramework;
using Library;

namespace LabManager.UserControls.Tabs.Products
{
    public partial class TabPackageInsert : TabTemplate
    {
        private BindingListView<PackageInsert> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabPackageInsert()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadPackageInsert();
        }

        private void SetTexts()
        {
            //TODO
        }

        private void LoadPackageInsert()
        {
            // Get new entities from view
            bindingList = new BindingListView<PackageInsert>(DataConnectorLayer.Instance.R_PackageInsert.FindAll(x => x.PackageInsertID > 0).ToList<PackageInsert>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(null, null);
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                tbDescription.Text = selectedRow.Cells[1].Value.ToString();
                tbVersion.Text = selectedRow.Cells[2].Value.ToString();
                tbFilepath.Text = selectedRow.Cells[3].Value.ToString();
                dtReleaseDate.Value = DateTime.Parse(selectedRow.Cells[4].Value.ToString());

                // Delete button
                var canDeleteProduct = DataConnectorLayer.Instance.R_Product.FindOne(x => x.PackageInsertID == selectedID) == null;

                btDelete.Enabled = canDeleteProduct;

                // Update
                btUpdate.Enabled = true;
            }
            else
            {
                tbDescription.Text = string.Empty;
                tbVersion.Text = string.Empty;
                tbFilepath.Text = string.Empty;
                dtReleaseDate.Value = DateTime.Now;

                btUpdate.Enabled = false;
                btDelete.Enabled = false;
            }

            // New button
            var canCreateManufacturer = DataConnectorLayer.Instance.R_Manufacturer.Set<Manufacturer>().Count() > 0;
            var canCreateProductCategory = DataConnectorLayer.Instance.R_ProductCategory.Set<ProductCategory>().Count() > 0;
            btNew.Enabled = canCreateManufacturer && canCreateProductCategory;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var item = new PackageInsert() { Description = Helper.EMPTY_STRING, Version = Helper.EMPTY_STRING, FilePath = string.Empty, ReleaseDate = DateTime.Now};
            DataConnectorLayer.Instance.R_PackageInsert.Create(item);
            DataConnectorLayer.Instance.R_PackageInsert.SaveChanges();
            LoadPackageInsert();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_PackageInsert.FindById(selectedID);
            DataConnectorLayer.Instance.R_PackageInsert.Delete(item);
            DataConnectorLayer.Instance.R_PackageInsert.SaveChanges();
            LoadPackageInsert();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_PackageInsert.FindById(selectedID);

            item.Description = tbDescription.Text;
            item.Version = tbVersion.Text;
            item.FilePath = tbFilepath.Text;
            item.ReleaseDate = dtReleaseDate.Value;

            DataConnectorLayer.Instance.R_PackageInsert.Update(item);
            DataConnectorLayer.Instance.R_PackageInsert.SaveChanges();
            LoadPackageInsert();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(PackageInsert packageInsert)
            {
                return  packageInsert.Description.ToLower().Contains(search) ||
                        packageInsert.Version.ToLower().Contains(search) ||
                        packageInsert.FilePath.ToLower().Contains(search) ||
                        packageInsert.ReleaseDate.ToString().ToLower().Contains(search);
            });
        } 

        private void btBrowse_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                tbFilepath.Text = openFileDialog.FileName;
                var tooltip = new ToolTip();
                tooltip.SetToolTip(tbFilepath, openFileDialog.FileName);
            }
        }

        #endregion

    }
}
